package cn.edu.scau.cmi.ema.service;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Crawler;
import cn.edu.scau.cmi.ema.domain.Crawlerpicture;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.*;
@Service("CrawlerpictureService")
@Transactional

public class CrawlerpictureServiceImpl extends CmiEmaService implements CrawlerpictureService {
	public CrawlerpictureServiceImpl() {
	}

	@Transactional
	public Crawlerpicture saveCrawlerpicture(Crawlerpicture crawlerpicture) {

		if (crawlerpicture.getId() == null) {
			Crawler crawler = crawlerDAO.findCrawlerById(crawlerpicture.getCrawler().getId());

			Crawlerpicture existingCrawlerpicture = crawlerpictureDAO
					.findCrawlerpictureByPhysicalPrimaryKey(crawlerpicture.getPath(), crawler.getId());

			crawlerpicture.setCrawler(crawler);

			if (isEquals(crawlerpicture, existingCrawlerpicture)) {
				return existingCrawlerpicture;
			}

			crawlerpicture = crawlerpictureDAO.store(crawlerpicture);
			crawlerpictureDAO.flush();

		} else {
			Crawlerpicture existingCrawlerpicture = crawlerpictureDAO
					.findCrawlerpictureByPrimaryKey(crawlerpicture.getId());

			crawlerpicture = crawlerpictureDAO.store(crawlerpicture);
			crawlerpictureDAO.flush();
		}

		return crawlerpicture;
	}

	public void deleteCrawlerpicture(Crawlerpicture crawlerpicture) {
		crawlerpictureDAO.remove(crawlerpicture);
		crawlerpictureDAO.flush();
	}

	@Transactional
	public void deleteCrawlerpictures(Set<Integer> deleteCrawlerpicturesId) {
		Query query = crawlerpictureDAO.createNamedQuery("deleteCrawlerpicturesById", -1, -1);
		query.setParameter(1, deleteCrawlerpicturesId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Crawlerpicture> loadCrawlerpictures() {
		return crawlerpictureDAO.findAllCrawlerpictures();

	}

	@Transactional
	public Set<Crawlerpicture> loadReferenceCrawlerpictures(Set<Crawlerpicture> crawlerpictures) {
		Iterator<Crawlerpicture> crawlerpicturesIterator = crawlerpictures.iterator();
		Set<Crawlerpicture> referencedCrawlerpictures = new HashSet<>();
		while (crawlerpicturesIterator.hasNext()) {
			Crawlerpicture currentCrawlerpicture = crawlerpicturesIterator.next();

		}
		return referencedCrawlerpictures;
	}

	public Set<Crawlerpicture> loadCrawlerpicturesByIdSet(Set<Integer> crawlerpictureIdSet) {

		Iterator<Integer> crawlerpictureIdIterator = crawlerpictureIdSet.iterator();
		Set<Crawlerpicture> crawlerpictureSet = new HashSet<>();
		while (crawlerpictureIdIterator.hasNext()) {
			Integer id = crawlerpictureIdIterator.next();
			Crawlerpicture currentCrawlerpicture = crawlerpictureDAO.findCrawlerpictureById(id);
			crawlerpictureSet.add(currentCrawlerpicture);
		}
		return crawlerpictureSet;
	}

	@Transactional
	public List<Crawlerpicture> findAllCrawlerpictures(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Crawlerpicture>(crawlerpictureDAO.findAllCrawlerpictures(startResult, maxRows));
	}

	@Transactional
	public Crawlerpicture findCrawlerpictureByPrimaryKey(Integer id) {
		return crawlerpictureDAO.findCrawlerpictureByPrimaryKey(id);
	}

	@Transactional
	public Integer countCrawlerpictures() {
		return ((Long) crawlerpictureDAO.createQuerySingleResult("select count(o) from Crawlerpicture o")
				.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countCrawlerpictures(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Crawlerpicture.class);
		return ((Long) crawlerpictureDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 *
	 */
	@Transactional
	public Integer countCrawlerpicturesByName(String queryName) {
		Query query = crawlerpictureDAO.createNamedQuery("countCrawlerpicturesByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countCrawlerpicturesByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerpicture.class);
		Session session = (Session) entityManager.getDelegate();
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadCrawlerpictureObjectReferencedSet(Crawlerpicture crawlerpicture) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();
		if (crawlerpicture.getCrawler() != null) {

			Set<Integer> crawlerIds = new HashSet<Integer>();
			crawlerIds.add(crawlerpicture.getCrawler().getId());
			objectReferencedSetMap.put("crawlerIds", crawlerIds);
		}

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadCrawlerpictureClassReferencedSet(Crawlerpicture crawlerpicture) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();
		classReferencedSetMap.put("crawlers", crawlerDAO.findAllCrawlers());

		return classReferencedSetMap;
	}

	public boolean isEquals(Crawlerpicture crawlerpicture, Object obj) {
		if (obj == crawlerpicture)
			return true;
		if (!(obj instanceof Crawlerpicture))
			return false;
		Crawlerpicture comparedCrawlerpicture = (Crawlerpicture) obj;
		if (!CheckPropertyEqual(crawlerpicture.getPath(), comparedCrawlerpicture.getPath())) {
			return false;
		}
		if (!CheckPropertyEqual(crawlerpicture.getCrawler().getId(), comparedCrawlerpicture.getCrawler().getId())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

